package com.gin.no_gay_alliance.fragment.forum;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gin.nga.response.forum.ForumGroup;
import com.gin.nga.response.forum.ForumSubGroup;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.adpter.forum.ForumSubGroupAdapter;
import com.gin.no_gay_alliance.viewmodel.EntrySubGroup;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * 首页版面分组
 */
public class ForumIndexFragment extends Fragment {
    private final static String TAG = ForumIndexFragment.class.getSimpleName();
    /**
     * 子组
     */
    private final ArrayList<ForumSubGroup> subGroups = new ArrayList<>();
    /**
     * 版面分组
     */
    private ForumGroup group;
    private RecyclerView rv;

    private ForumSubGroupAdapter forumSubGroupAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_forum_index, container, false);
        rv = view.findViewById(R.id.recycler_view);
        forumSubGroupAdapter = new ForumSubGroupAdapter(new ArrayList<>(), requireActivity());
        rv.setLayoutManager(new LinearLayoutManager(requireActivity()));
        rv.setAdapter(forumSubGroupAdapter);
        rv.addItemDecoration(new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL));
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        forumSubGroupAdapter.setData(subGroups.stream().map(EntrySubGroup::new).collect(Collectors.toList()));
    }

    public void setForumGroup(ForumGroup group) {
        this.group = group;
        this.subGroups.clear();
        this.subGroups.addAll(group.getSubGroups().values());
    }
}